package org.jc.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * 功能描述
 *
 * @author wanghongqian
 * @date 2024/06/17 09:29
 */
public class DruidEncryptUtil {

    private static String publicKey;

    private static String privateKey;

    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            privateKey = keyPair[0];
            System.out.println("privateKey:"+privateKey);
            publicKey = keyPair[1];
            System.out.println("publicKey:"+publicKey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }

    public static String encrypt(String plainText) throws Exception {
        String encrypt = ConfigTools.encrypt(privateKey,plainText);
        return encrypt;
    }

    public static String decrypt(String plainText) throws Exception {
        String decrypt = ConfigTools.decrypt(publicKey,plainText);
        return decrypt;
    }

    public static void main(String[] args) throws Exception {
        String encrypt = ConfigTools.decrypt(
                "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJO52FMlmDpHIhWHgVhyJQlkjReaPuap7crMcBK1pFcNHB1wIXbVzAx/UYGI0a6fh+qycyDvJhdfcxnBqlSoG2UCAwEAAQ==",
                "CYfM1bCaYk0KnNq6Lvl5lEznW659x6ooK19o4uD//4NR1GQTZW2oeG4bmofSf56IgbIrO/DONLxVO+tGtE5PJg==");
        System.out.println("encrypt:"+encrypt);
    }

}
